<?php
/*
 * 维修保养
 * @package    auto
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 458259cfccb1b948c8bcaeb1813a624bd2b3aff8 $
 */
defined('IN_IXDCW') or exit('Access Denied');

/**
 * 维修保养
 * Class MaintainceModule
 */
class MaintainceModule extends BasicModule {

	/** @var 模块id */
	private $_moduleid;

	/** @var 条目ID */
	private $_itemid;

	/** @var string 表格 */
	private $_table;

	/** @var string 表格数据 */
	private $_tableData;

	/** @var 是否内容分表 */
	private $_split;

	/** @var array 可用字段 */
	private $_fields;

	/**
	 * constructor
	 * @param $moduleid
	 */
	function __construct($moduleid) {
		parent::__construct();
		global $table, $tableData, $MOD;
		$this->_moduleid  = $moduleid;
		$this->_table     = $table;
		$this->_tableData = $tableData;
		$this->_split     = $MOD['split'];
		$this->_fields    = array(
			'aid',
			'catid',
			'arbid',
			'armid',
			'arcid',
			'armid',
			'title',
			'level',
			'typeid',
			'status',
			'hits',
			'username',
			'addtime',
			'editor',
			'edittime',
			'ip',
			'linkurl',
			'note'
		);
	}

	public function setItemid($itemid) {
		$this->_itemid = $itemid;
	}

	public function getItemid() {
		return $this->_itemid;
	}

	public function getFields() {
		return $this->_fields;
	}

	/**
	 * 是否通过插入检测
	 * @param $post
	 * @return bool
	 */
	function pass($post) {
		if (!is_array($post)) return false;
		if (!$post['arbid']) return $this->err('请选择品牌');
		if (strlen($post['title']) < 3) return $this->err('请填写标题');
		if (!$post['content']) return $this->err('请填写内容');
		return true;
	}

	/**
	 * 设置符合内容的值
	 * @param $post
	 * @return array|mixed
	 */
	function set($post) {
		global $MOD, $DT_TIME, $DT_IP, $_username, $_userid;
		$thumbSeName = $this->_moduleid.'_thumb';
		$post['addtime']  = (isset($post['addtime']) && $post['addtime']) ? strtotime($post['addtime']) : $DT_TIME;
		$post['edittime'] = $DT_TIME;
		$post['title']    = trim($post['title']);
		$post['content']  = stripslashes($post['content']);
		if ($post['content'] && isset($post['clear_link']) && $post['clear_link']) {
			$post['content'] = Post::clearLink($post['content']);
		}
		if ($post['content'] && isset($post['save_remotepic']) && $post['save_remotepic']) {
			$post['content'] = Post::saveRemote($post['content']);
		}
		if ($post['content'] && $post['thumb_no'] && !$post['thumb']) {
			$post['thumb'] = Post::saveThumb($post['content'], $post['thumb_no'], $MOD['thumb_width'], $MOD['thumb_height']);
		}
		if (strpos($post['content'], 'pagebreak') !== false) {
			$post['content'] = str_replace(array('[pagebreak]</p>', '<p>[pagebreak]', '[pagebreak]</div>', '<div>[pagebreak]'), array('</p>[pagebreak]', '[pagebreak]<p>', '</div>[pagebreak]', '[pagebreak]<div>'), $post['content']);
		}
		if ($post['content'] && !$post['introduce'] && $post['introduce_length']) {
			$post['introduce'] = addslashes(Post::intro($post['content'], $post['introduce_length']));
		}

		if ($this->_itemid) {
			// 修改信息
			$r   = $this->getOne();
			$post['editor'] = $_username;
			$new            = $post['content'];
			$old = $r['content'];
			EditorUpload::clearDiff($new, $old, $_userid);
		} else {
			$post['username'] = $post['editor'] = $_username;
			$post['ip']       = $DT_IP;
		}
		if (!defined('DT_ADMIN')) {
			$content = $post['content'];
			unset($post['content']);
			$post            = Str::htmlSpecialChars($post);
			$post['content'] = Str::safe($content);
		}
		$post['content'] = addslashes($post['content']);
		return $post;
	}

	/**
	 * 获取一条数据
	 * @return array|void
	 */
	function getOne() {
		$content_table = content_table($this->_moduleid, $this->_itemid, $this->_split, $this->_tableData);
		return $this->_db->getOne("SELECT * FROM {$this->_table} a,{$content_table} c WHERE a.itemid=c.itemid and a.itemid=$this->_itemid");
	}

	/**
	 * 获取列表
	 * @param string $condition
	 * @param string $order
	 * @param string $cache
	 * @return array
	 */
	function getList($condition = 'status=3', $order = 'addtime DESC', $cache = '') {
		global $MOD,$items;
		$num = $this->_db->count($this->_table, $condition, $cache);
		$items=$num;
		$this->_pn->recordsPerPage($this->_pagesize);
		$this->_pn->records($num);
		$sql = "SELECT * FROM {$this->_table}";
		$sql .= $condition ? " WHERE {$condition}" : '';
		$sql .= $order ? " ORDER BY {$order}" : '';
		$sql .= " LIMIT " . ($this->_pn->getPage() - 1) * $this->_pagesize . ',' . $this->_pagesize;
		$lists  = $catids = $CATS = array();
		$result = $this->_db->query($sql, $cache);
		while ($r = $this->_db->fetchArray($result)) {
			$r['adddate']  = timetodate($r['addtime'], 5);
			$r['editdate'] = timetodate($r['edittime'], 5);
			$r['alt']      = $r['title'];
			$r['title']    = set_style($r['title'], $r['style']);
			$catids[$r['catid']] = $r['catid'];
			$lists[]             = $r;
		}
		return $lists;
	}

	/**
	 * @param $post
	 * @return int|string
	 */
	function add($post) {
		$post = $this->set($post);
		$this->_db->query(Sql::insert($this->_table, $post, $this->_fields));
		$this->_itemid = $this->_db->insertId();
		$content_table = content_table($this->_moduleid, $this->_itemid, $this->_split, $this->_tableData);
		$this->_db->query("INSERT INTO {$content_table} (itemid,content) VALUES ('$this->_itemid', '$post[content]')");
		$this->update($this->_itemid, $post, $post['content']);
		return $this->_itemid;
	}

	/**
	 * 编辑
	 * @param $post
	 * @return bool
	 */
	function edit($post) {
		$post = $this->set($post);
		$this->_db->query(Sql::update($this->_table, $post, "itemid={$this->_itemid}", '', $this->_fields));
		$content_table = content_table($this->_moduleid, $this->_itemid, $this->_split, $this->_tableData);
		$this->_db->query(Sql::update($content_table, "content='$post[content]'", "itemid=$this->_itemid"));
		$this->update($this->_itemid, $post, $post['content']);
		return true;
	}

	/**
	 * 更新
	 * @param        $itemid
	 * @param array  $item
	 */
	function update($itemid, $item = array()) {
		$item or $item = $this->_db->getOne("SELECT * FROM {$this->_table} WHERE itemid=$itemid");
		$item['itemid'] = $itemid;
		$linkurl        = url('mantaince/show', 'itemid='.$itemid);
		$sql            = "linkurl='$linkurl'";
		$this->_db->query(Sql::update($this->_table,"$sql","itemid=$itemid"));
	}

	/**
	 * 回收站
	 * @param $itemid
	 * @return bool
	 */
	function recycle($itemid) {
		if (is_array($itemid)) {
			foreach ($itemid as $v) {
				$this->recycle($v);
			}
		} else {
			$this->_db->query(Sql::update($this->_table,"status=0","itemid=$itemid"));
			$this->delete($itemid, false);
			return true;
		}
		return true;
	}

	/**
	 * 还原为默认状态
	 * @param $itemid
	 * @return bool
	 */
	function restore($itemid) {
		global $module, $MOD;
		if (is_array($itemid)) {
			foreach ($itemid as $v) {
				$this->restore($v);
			}
		} else {
			$this->_db->query(Sql::update($this->_table,"status=3","itemid=$itemid"));
			return true;
		}
		return true;
	}

	/**
	 * 删除条目
	 * @param      $itemid
	 * @param bool $all
	 */
	function delete($itemid, $all = true) {
		if (is_array($itemid)) {
			foreach ($itemid as $v) {
				$this->delete($v, $all);
			}
		} else {
			$this->_itemid = $itemid;
			$r             = $this->getOne();
			if ($all) {
				$userid = get_user($r['username']);
				if ($r['thumb']) PictureUpload::deletePicture($r['thumb'], $userid);
				if ($r['content']) EditorUpload::clearDiff('', $r['content'], $userid);
				$this->_db->query(Sql::delete($this->_table,"itemid=$itemid"));
				$content_table = content_table($this->_moduleid, $this->_itemid, $this->_split, $this->_tableData);
				$this->_db->query(Sql::delete($content_table,"itemid=$itemid"));
			}
		}
	}

	/**
	 * 通过审核
	 * @param $itemid
	 * @return bool
	 */
	function check($itemid) {
		global $_username, $DT_TIME;
		if (is_array($itemid)) {
			foreach ($itemid as $v) {
				$this->check($v);
			}
		} else {
			$this->_itemid = $itemid;
			$this->_db->query(Sql::update($this->_table,"status=3,hits=hits+1,editor='$_username',edittime=$DT_TIME","itemid=$itemid"));
			return true;
		}
		return true;
	}

	/**
	 * 未通过审核
	 * @param $itemid
	 * @return bool
	 */
	function reject($itemid) {
		global $_username;
		if (is_array($itemid)) {
			foreach ($itemid as $v) {
				$this->reject($v);
			}
		} else {
			$this->_db->query(Sql::update($this->_table,"status=1,editor='$_username'","itemid=$itemid"));
			return true;
		}
		return true;
	}

	/**
	 * 全部删除回收站里的内容
	 * @param string $condition
	 */
	function clear($condition = 'status=0') {
		$result = $this->_db->query("SELECT itemid FROM {$this->_table} WHERE $condition ");
		while ($r = $this->_db->fetchArray($result)) {
			$this->delete($r['itemid']);
		}
	}

	/**
	 * 级别设置
	 * @param $itemid
	 * @param $level
	 */
	function level($itemid, $level) {
		$itemids = is_array($itemid) ? implode(',', $itemid) : $itemid;
		$this->_db->query(Sql::update($this->_table,"level=$level","itemid IN ($itemids)"));
	}
}